package com.atguigu.day11;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink01_SQL_GroupWindow {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);

// 作为事件时间的字段必须是 timestamp 类型, 所以根据 long 类型的 ts 计算出来一个 t
        tEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int, " +
                "t as to_timestamp(from_unixtime(ts/1000,'yyyy-MM-dd HH:mm:ss'))," +
                "watermark for t as t - interval '5' second)" +
                "with("
                + "'connector' = 'filesystem',"
                + "'path' = 'input/sensor-sql.txt',"
                + "'format' = 'csv'"
                + ")");

        //TODO 使用GroupWindow
        //滚动窗口
//        tEnv.executeSql("select " +
//                "id," +
//                "sum(vc)," +
//                "Tumble_start(t,interval '3' second) as wStart," +
//                "Tumble_end(t,interval '3' second) as wEnd " +
//                "from sensor " +
//                "group by " +
//                "id," +
//                "Tumble(t,interval '3' second)").print();

        //滑动窗口
        tEnv.executeSql("select " +
                "id," +
                "sum(vc)," +
                "hop_start(t,interval '2' second,interval '3' second) as wStart," +
                "hop_end(t,interval '2' second,interval '3' second) as wEnd " +
                "from sensor " +
                "group by " +
                "id," +
                "hop(t,interval '2' second,interval '3' second)").print();

    }
}
